var common = require('../../../utils/common.js');
var app = getApp();
var baseUrl = app.url;

Page({
    data: {
        inputShowed: false,
        inputVal: "",
        files: [],
        datalist:{},
        details:{},
        typeIndex:0,  
        type:['请选择','以租代购','自有产权','全款购置'],
        descNum:0,
        descDigit:0      
    },
    onLoad:function () {
        myDate = new Date();
        Y = myDate.getFullYear();
        M = myDate.getMonth();
        m = ++ M;
        d = myDate.getDate();
        m=checkTime(m);
        d=checkTime(d);
        function checkTime(i){
            if (i<10) {
                i="0" + i
            }
            return i
        }
        this.setData({bug_date:Y+'-'+m+'-'+d,repair_date:Y+'-'+m+'-'+d,in_time:Y+'-'+m+'-'+d,insure_time:Y+'-'+m+'-'+d})
    },
    bindBugChange: function (e) {
        this.setData({
            bug_date: e.detail.value
        })
    },
    bindRecordChange: function (e) {
        this.setData({
            repair_date: e.detail.value
        })
    },
    bindEnterChange: function (e) {
        this.setData({
            in_time: e.detail.value
        })
    },
    bindInsureChange: function(e) {
        this.setData({
            insure_time: e.detail.value
        })
    },
    inputChange:function(event){
        var e = event.detail.value
        if(e.length>0){
            common.bestCurl('GET','/record/ajaxgetcarsn',res=>{
                if(res.statusCode == 200){
                    if(res.data.length!=0){
                        this.setData({ datalist : res.data,inputShowed:true}) 
                    }else{
                        this.setData({datalist : {},inputShowed:false}) 
                    }
                }
            },{'query':e})
        }else{
            setTimeout(res=>{
                    this.setData({datalist : {},inputShowed:false});
            },300)         
        };
    },
    onCarSn:function(event){
        if(this.data.datalist.length){
            var car_sn = event.target.id;
            this.setData({inputVal:car_sn,inputShowed: false})
        }        
    }, 
    onCarUnique:function(event){
        if(this.data.details.length){
            var car_unique_id = event.currentTarget.id;
            this.setData({car_unique_id:car_unique_id,carUniqueShowed: false})
        }        
    }, 
    chooseImage: function (e) {
        var that = this;
        wx.chooseImage({
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success: function (res) {
                that.setData({
                    files: that.data.files.concat(res.tempFilePaths)
                });
            }
        })
    },
    previewImage: function(e){
        wx.previewImage({
            current: e.currentTarget.id,
            urls: this.data.files
        })
    },
    clickWrongIcon:function(event){
        id = event.target.id;
        var obj = this.data.files;
        obj.splice(id,1);
        this.setData({files:obj})
    },
    descChange:function (event) {
        this.setData({descNum:event.detail.cursor});   
    }, 
    DigitChange:function(event){
        this.setData({descDigit:event.detail.cursor});
    },
    formSubmit:function (event) {
        var formData = event.detail.value;
        var inputVal = this.data.inputVal;
        if(inputVal.length == 0){
            wx.showModal({
                content: '请添加车牌号',
                showCancel: false,
            });  
        }else{
                common.bestCurl('POST','/repair/add',res=>{
                if(res.statusCode == 200){
                    if(res.data.code == 1){
                        var id =res.data.id;
                        var filesUrl =this.data.files;
                        var flag = 0;
                        if(filesUrl.length>0){
                            wx.showLoading({
                                title: '图片上传中',
                                icon: 'loading',
                                mask:true
                            });
                            let SESSID = wx.getStorageSync('PHPSESSID');
                            if(SESSID){
                                var PHPSESSID = '?PHPSESSID='+SESSID;
                            }else{
                                PHPSESSID = '?';
                            }
                            url=baseUrl+'/repair/add_file'+PHPSESSID;
                            for (var i = filesUrl.length - 1; i >= 0; i--) {
                                wx.uploadFile({
                                    url: url,
                                    filePath: filesUrl[i],
                                    name: 'uploadfile',
                                    formData:{
                                        'id': id
                                    },
                                    success: function(res){                        
                                        flag++;
                                        if(filesUrl.length == flag){
                                            wx.hideLoading()
                                            wx.showToast({
                                                title: '添加成功',
                                                icon: '',
                                                duration: 5000
                                            });
                                            setTimeout(res=>{
                                                wx.redirectTo({
                                                    url: '../list/list'
                                                })
                                            },2000)
                                        }   
                                    }
                                });
                            }
                        }else{
                            wx.showToast({
                                title: '添加成功',
                                icon: '',
                                duration: 5000
                            });
                            setTimeout(res=>{
                                wx.redirectTo({
                                    url: '../list/list'
                                })
                            },2000)
                        }
                    }else if(res.data.code == 2){
                        wx.showModal({
                            content: '车牌号不存在,请查证后再提交',
                            showCancel: false,
                        });
                    }
                }
            },formData)
        }
    }
});

